﻿Ext.namespace("Appframe.common");

// 传到后台排序的数据   
Appframe.common.getSortIndexDate=function (xh,store,tablename,primaryKeyName,sortFieldname) {   

//for (var i=0;i<xh.length;i++)
//alert(xh[i])


             
                    var jsondata = "";
					var i=0;
                   store.each(function(record) { 
				   
				 if (jsondata.length > 0) 
		      	jsondata += '&';
		       
		        jsondata += "primaryKeyValue=" + record.get(primaryKeyName)+"&xh="+xh[i];  
				 store.getAt(i).set(sortFieldname,xh[i]);
				  i++;
							  
					 }); 


	
   if (jsondata.length > 0)  
jsondata +="&tablename="+tablename+"&primaryKeyName="+primaryKeyName+"&sortFieldname="+sortFieldname;

//alert(jsondata)

//return;

               Ext.Ajax.request({
								url :root+'comm.do?method=updateSortData&'+jsondata,									
								waitMsg : '正处理中...',//遮罩
								success : function(response,options) {																	
									//store.reload();
								},
								failure :function(response,options) {
								Ext.MessageBox.alert('失败', Ext.decode(response.responseText).message)
								}
							});
         
       
  
          
      }  



Appframe.common.getDDrow=function (dataGrid,tablename,primaryKeyName,sortFieldname,ddGroupname) { 
	
primaryKeyName=primaryKeyName.toLowerCase();
sortFieldname=sortFieldname.toLowerCase();
//alert(sortFieldname)

 var ddrow = new Ext.dd.DropTarget(dataGrid.getEl(), {
        ddGroup: ddGroupname,
        copy: false,
        notifyDrop: function (dd, e, data) {
            // 选中了多少行
            debugger
            var rows = data.selections;
            // 拖动到第几行
            var index = dd.getDragData(e).rowIndex;
			//alert(index)
            if (typeof (index) == "undefined") {
                return;
            }
			
			var store =dataGrid.getStore();
			
			var xh=[];
			store.each(function(record) {
//alert(record.get(sortFieldname))
xh.push(record.get(sortFieldname));
 }); 
			
            // 修改store
            for (i = 0; i < rows.length; i++) {
                var rowData = rows[i];
               if (!this.copy) store.remove(rowData);
                store.insert(index, rowData);
            }
			
			Appframe.common.getSortIndexDate(xh,store,tablename,primaryKeyName,sortFieldname);
			
        }
    });

       return ddrow;
}


Appframe.common.movEvent=function(ds,pageToolbar,f,currecord,forwarrecord,tablename,primaryKeyName,sortFieldname) {
var sortEvent=function(obj,pageData) { //change
			if (f==0){					 
              if (ds.data.length > 0)
			  {
			 forwarrecord= ds.getAt(ds.data.length-1);
			 ds.removeAt(ds.data.length-1);
			 ds.add(currecord);
			
			//store.getAt(ds.data.length-1).set("JSYXM",grid.getStore().getAt(rowIndex).data.name); 
			  }
			} else
			{
			forwarrecord= ds.getAt(0);			
			 ds.removeAt(0);
			 ds.insert(0,currecord);
				
			}
			// this.un("change", ml,  this);    
  
			 
			  var jsondata = "primaryKeyValue="+currecord.get(primaryKeyName)+"&xh="+forwarrecord.get(sortFieldname)+"&primaryKeyValue="+forwarrecord.get(primaryKeyName)+"&xh="+currecord.get(sortFieldname)+"&tablename="+tablename+"&primaryKeyName="+primaryKeyName+"&sortFieldname="+sortFieldname;
			// alert(jsondata);
			// return;
			    Ext.Ajax.request({
								url :root+'comm.do?method=updateSortData&'+jsondata,									
								waitMsg : '正处理中...',//遮罩
								success : function(response,options) {								
									pageToolbar.un('change', sortEvent);
									ds.reload();
								},
								failure :function(response,options) {
								Ext.MessageBox.alert('失败', Ext.decode(response.responseText).message)
								}
							});
			 
			// alert(currecord.data.xh+","+forwarrecord.data.xh)
 
            }//change

    pageToolbar.on('change', sortEvent);
	}


//排序
 Appframe.common.buttonMove=function (grid, pageToolbar, sortFieldname, toward){  
		var temp_record = grid.getSelectionModel().getSelected();
		var temp_fbh = temp_record.get("fbh");
		var temp_ffloor = temp_record.get("ffloor");
		var temp_forder = temp_record.get("forder");
	 	sortFieldname=sortFieldname.toLowerCase();
        var sm = grid.getSelectionModel();   
        var data = sm.getSelections();   
        if(sm.hasSelection()){   
            var rec = sm.getSelected();   
            var ds = grid.getStore();  
			
			var xh=[];
			ds.each(function(record) {
				//alert(record.get(sortFieldname))
				xh.push(record.get(sortFieldname));
		    }); 
			
            var rowIndex = ds.indexOf(rec) + toward;   
            var ri=[];   
            for(var i=1;i<data.length;i++){   
                var index = ds.indexOf(data[i]);   
                ri.push(index);   
            }   
            if(toward==1){   
                ri.sort(function(x,y){   
                    if(parseInt(x)>parseInt(y))   
                        return 1;   
                    else  
                        return -1;   
                })   
            } else {   
                ri.sort(function(x,y){   
                    if(parseInt(x)>parseInt(y))   
                        return -1;   
                    else  
                        return 1;   
                })   
            }  
            
            for(var i=0;i<ri.length;++i) {   
                if(rowIndex == ri[i])   
                    rowIndex += toward;   
            }   
            if(rowIndex < 0){ 
				var currecord =null,forwarrecord =null;
				if (rowIndex > 0) {
					var sstt = grid.getSelectionModel().getSelected();
					var str = "";
	               	ds.each(function(record) {
	       				str += "" + record.get("fid") + ",";
	       		    });
	               	if (str.length== 0) {
	               		str = "";
					} else {
						str = str.substring(0, str.length - 1);
					}
	               	
	               	//**************************************************
					//return;
	               	Ext.Ajax.request({
						url : 'headInfo!orderHeadInfo.do',
						method : 'GET',
						params : {
							"fbh" : temp_fbh,
							"ffloor" : temp_ffloor,
							"forder" : temp_forder,
							"ftoward" : toward
						},
						success : function(response, action) {
							
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '排序发生错误！',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR
							});
						}
					});
					//currecord= ds.getAt(0);
				} else {
					Ext.Msg.alert('提示','记录已在顶部！');   
				}
            } else  
                if(rowIndex >= ds.getCount()){
                	
                	Ext.Msg.alert('提示','记录已在底部！');   
                } else {   
                   Appframe.common.move(grid,rowIndex,data); 
                   var str = "";
	               	ds.each(function(record) {
	       				str += "" + record.get("fid") + ",";
	       		    });
	               	if (str.length== 0) {
	               		str = "";
					} else {
						str = str.substring(0, str.length - 1);
					}
	               	
	               	//**************************************************
					//return;
	               	Ext.Ajax.request({
						url : 'headInfo!orderHeadInfo.do',
						method : 'GET',
						params : {
							"fbh" : temp_fbh,
							"ffloor" : temp_ffloor,
							"forder" : temp_forder,
							"ftoward" : toward
						},
						success : function(response, action) {
							//alert(response.responseText);
							var mess = response.responseText;
							if(mess == "err") {
								Ext.Msg.alert('提示','当前排序会造成报表格式错误，排序失败!');   
							}
						},
						failure : function() {
							Ext.Msg.alert('提示','当前排序会造成报表格式错误，排序失败!');  
						}
					});
                }   
        } else{   
            Ext.Msg.alert('提示','请选择记录！');   
        }   
    };   


 Appframe.common.move=function (grid,rindex,data){   
        var ds = grid.getStore();   
        var rdata = ds.getAt(rindex);   
        var toward = 0;   
        var index = ds.indexOf(data[0]);   
        if(rindex > index)   
            toward = 1;   
        for(var i=0;i<data.length;i++){   
            ds.remove(data[i]);   
        }   
        rindex = ds.indexOf(rdata) + toward;   
        for(var i=0;i<data.length;i++){   
            ds.insert(rindex,data[i]);   
            rindex++;   
        }   
        var sm = grid.getSelectionModel();   
        if(sm)   
            sm.selectRecords(data);   
    }  
